﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asp
{
    public partial class _Default : Page
    {
        private DataTable Data
        {
            get
            {
                var sql = "select * from Articles";
                var dt = DbHelper.GetDataTable(sql);
                return dt;
            }
        }
        private void FillData()
        {

            gv1.DataSource = this.Data;
            gv1.DataBind();
        }
        private string GetValue(int rowIndex, int colIndex)
        {
            var c = gv1.Rows[rowIndex].Cells[colIndex];
            var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;
            return value;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            FillData();
        }

        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where id='{0}'", id);
            DbHelper.Exc(sql);
            gv1.EditIndex = -1;
            FillData();
        }

        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

                var iddString = GetValue(e.RowIndex, 0);
                var isOk = int.TryParse(iddString, out int id);
                var Title = GetValue(e.RowIndex, 1);
                var Content = GetValue(e.RowIndex, 2);
                var Author = GetValue(e.RowIndex, 3);
                var Remarks = GetValue(e.RowIndex, 4);
                if (isOk)
                {
                    var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}'，Remarks='{3}' where id='{3}'", Title, Content, Author, Remarks, id);
                    DbHelper.Exc(sql);
                }
                else
                {
                    var sql = string.Format("insert into Articles(Title,Content,Author,Remarks)values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                    DbHelper.Exc(sql);
                }
                gv1.EditIndex = -1;
                FillData();
        }
            
        

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var tempDt = this.Data;
            var dt = tempDt.Copy();
            dt.Rows.Add(dt.NewRow());
            gv1.DataSource = dt;
            gv1.DataBind();
            gv1.EditIndex = dt.Rows.Count - 1;
            gv1.DataSource = dt;
            gv1.DataBind();
            tntCommret.Text = "请勿自己添加或者修改Id！！！";
        }
    }
}